<?php

namespace Xrfg\Wallet\CollectERC\Console;

use App\Models\Coin;
use App\Models\Recharge;
use Illuminate\Console\Command;
use Xrfg\Wallet\CollectERC\Services\ETHWallet;

class ExportErc extends Command
{
    protected $signature = 'export_erc {coin_name}';

    protected $description = 'ERC20归集第二步：转出ERC';

    // 币种名称
    protected $coin_name;

    // 归集配置
    protected $config;

    protected $rpc;

    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $this->coin_name = $this->argument('coin_name');
        $coin = Coin::where('name', $this->coin_name)->first();
        if (!$coin) {
            return;
        }

        $this->config = config('wallet.eth.collect_coins')[$this->coin_name];
        $this->rpc = new ETHWallet($this->config);
        // 测试：转出 ERC
//        $this->rpc->exportErcStep2('0xed79575e18a8f0d66b9270e8335a35cd05c8bb76');
//        return;
        $addresses = $this->rpc->getAccounts();
        foreach ($addresses as $address) {
            if ($address == $this->config['mainAddress']) {
                continue;
            }
            // 当天充币，第二天归集
            if (Recharge::where('address', $address)->whereDate('created_at', today())->exists()) {
                continue;
            }
            $this->rpc->exportErcStep2($address);
        }
    }
}
